package com.wangmin.intercept.websetting;

import android.graphics.Bitmap;
import android.net.http.SslError;
import android.webkit.SslErrorHandler;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.wangmin.intercept.adapter.WebviewClientAdater;
import com.wangmin.intercept.listener.IWebViewClient;
import com.wangmin.intercept.listener.UrlAddedListener;
import com.wangmin.intercept.utils.LogUtils;

import java.util.List;
import java.util.Timer;
/**
 * Created by wm on 2016/11/11.
 */
public class CustomWebViewClient extends WebViewClient {
    private LogUtils logUtils = new LogUtils("CustomWebViewClient");
    private Timer timer = new Timer();
    private int firstOpen=0;
    private IWebViewClient iWebViewClient;
    public CustomWebViewClient(UrlAddedListener urlAddedListener){
        super();
        iWebViewClient = new WebviewClientAdater(urlAddedListener);
    }
    @Override
    public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
        super.onReceivedError(view, request, error);
    }
    public void setFirstOpen(int firstOpen){
        this.firstOpen=firstOpen;
    }
    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        super.onReceivedError(view, errorCode, description, failingUrl);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        return super.shouldOverrideUrlLoading(view, url);
    }

    public void setShareBypage(boolean shareBypage) {
        this.shareBypage = shareBypage;
    }

    private boolean shareBypage = false;

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        if(view instanceof CustomWebview) {
            CustomWebview customWebview = (CustomWebview) view;
            iWebViewClient.onPageFinished(customWebview, url);
        }
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        if(view instanceof CustomWebview) {
            CustomWebview customWebview = (CustomWebview) view;
            iWebViewClient.onPageStarted(customWebview, url,favicon);
        }
    }


    @Override
    public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
        if(view instanceof CustomWebview) {
            CustomWebview customWebview = (CustomWebview) view;
            WebResourceResponse webResourceResponse = iWebViewClient.shouldInterceptRequest(customWebview, url);
            if(webResourceResponse != null){
                return webResourceResponse;
            }
        }
        return super.shouldInterceptRequest(view, url);
    }

    @Override
    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
        handler.proceed();
    }

    /**
     * 取消线程
     */
    public void onDestory(){
        iWebViewClient.onDestory();
    }

    public void onStart(){
        iWebViewClient.onStart();
    }

    @Override
    public void doUpdateVisitedHistory(WebView view, String url, boolean isReload) {
        super.doUpdateVisitedHistory(view, url, isReload);
        if(view instanceof CustomWebview) {
            CustomWebview customWebview = (CustomWebview) view;
            iWebViewClient.doUpdateVisitedHistory(customWebview,url,isReload);
        }
    }

    public List<String> getUrls(){
        if(iWebViewClient !=null && iWebViewClient instanceof WebviewClientAdater){
            WebviewClientAdater adater = (WebviewClientAdater) iWebViewClient;
            return adater.getUrls();
        }
        return null;
    }

    public void clearInterceptUrl(){
        if(iWebViewClient instanceof WebviewClientAdater){
            WebviewClientAdater adater = (WebviewClientAdater) iWebViewClient;
            adater.clear();
        }
    }

    public void addInterceptUrl(String url){
        if(iWebViewClient instanceof WebviewClientAdater){
            WebviewClientAdater adater = (WebviewClientAdater) iWebViewClient;
            adater.addUrl(url);
        }
    }

    public void addInterceptUrl(List<String> url){
        if(iWebViewClient instanceof WebviewClientAdater){
            WebviewClientAdater adater = (WebviewClientAdater) iWebViewClient;
            adater.addUrl(url);
        }
    }
}
